#!/bin/bash
alias mv='mv' # 取消强制覆盖时的确认
alias echo='echo -e'
#/usr/bin/cp 6_mvdir_uid.sh /home/yyy/ && su - yyy -c "sh /home/yyy/6_mvdir_uid.sh 3"

SRCQUOTA="/${QUOTA_TEST}/srcQuotaDir"
Q_SRCDIR="${FS_MOUNTPOINT}${SRCQUOTA}" # Q_ 开头的目录代表创建了配额

DESTQUOTA="/${QUOTA_TEST}/destQuotaDir"
Q_DESTDIR="${FS_MOUNTPOINT}${DESTQUOTA}"

NOQUOTADIR="${FS_MOUNTPOINT}/${QUOTA_TEST}/noQuotaDir"
Q_CHILDDIR="${NOQUOTADIR}/childQuotaDir"
CHILDQUOTA="${QUOTA_TEST}/noQuotaDir/childQuotaDir"

USER="u1"; GROUP="u1"
Q_UID=`cat /etc/passwd | grep ${USER} | awk -F: '{print $3}'`
Q_GID=`cat /etc/passwd | grep ${USER} | awk -F: '{print $4}'`

function print_help() {
    name="$0"
    echo "Usage:"
    echo "  sh ${name} 0      测试所有用例"
    echo "  sh ${name} 1      rename时目标目录不能存在(vfs报错)"
    echo "  sh ${name} 2      不能 rename/mv 配额目录本身"
    echo "  sh ${name} 3      不能 rename/mv 配额目录的父目录"
    echo "  sh ${name} 4      源目录和目标目录都不属于配额目录" 
    echo "  sh ${name} 5      源目录和目标目录都被设置配额, 且属于同一配额目录"
    echo "  sh ${name} 6      源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额未超出"
    echo "  sh ${name} 7      源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额超出"
    echo "  sh ${name} 8      源目录属于配额目录，目标目录不属于配额目录，配额不会超出"
    echo "  sh ${name} 9      源目录不属于配额目录，目标目录属于配额目录，验证目标配额未超出"
    echo "  sh ${name} 10     源目录不属于配额目录，目标目录属于配额目录，验证目标配额超出"
    exit
}

function reset_all() {
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    func_cleandir ${Q_SRCDIR}
    func_cleandir ${Q_DESTDIR} 
    func_cleandir ${NOQUOTADIR}
    mkdir -p ${Q_SRCDIR} ${Q_DESTDIR} ${Q_CHILDDIR}
    chown -R u1:u1 ${FS_MOUNTPOINT}/${QUOTA_TEST}/

    orcafs-ctl --setquota --dir=${Q_SRCDIR} --hardlimit=10M --uid=${Q_UID} &> /dev/null
    orcafs-ctl --setquota --dir=${Q_DESTDIR} --hardlimit=8M --gid=${Q_GID} &> /dev/null
    orcafs-ctl --setquota --dir=${Q_CHILDDIR} --hardlimit=5M --uid=${Q_UID} &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};

    echo -n "=====清空目录并重新创建配额目录 [${SRCQUOTA}] [${DESTQUOTA}] [${CHILDQUOTA}] ====="
    quota_num=`orcafs-ctl --listquota | grep "DIR_" | wc -l`
    if ((${quota_num} == 3));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

echo "/************************************************************************"
echo "测试在不同<用户配额>目录内 rename/mv 目录"
echo "************************************************************************/"

if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
# init global quota env and check env
sh ${QUOTA_CHECK_ENV}
source ${QUOTA_COMMON_FUNC}
reset_all;

set -e
file_name=6_mvdir_uid_common.sh
env_file=0_source_env.sh
/usr/bin/cp -f ${QUOTA_TEST_PATH}/rename/${file_name} /home/${USER}/
/usr/bin/cp -f ${QUOTA_TEST_PATH}/0_source_env.sh /home/${USER}/

chown ${USER}:${USER} /home/${USER}/*
chmod 0755 /home/${USER}/${file_name}

su - ${USER} -c "sh /home/${USER}/${file_name} $1"
if (($? != 0)); then echo "\n=========测试失败\n";exit 1; fi

chown -R root:root ${FS_MOUNTPOINT}/${QUOTA_TEST}/
echo "/************************************************************************"
echo "测试在不同<用户配额>目录内 rename/mv 目录 ===== all success"
echo "************************************************************************/"
exit 0;